$(function () {
    var eSwiper = null;
    var examRecordId = $("#examRecordId").val();
    var status = $("#status").val();
    //加载答题卡
    loadCard(examRecordId,status);
    var index = 0;
    //加载第一页
    loadPageList(1,function () {
        eSwiper = examChangeFn(function (activeIndex) {
            var _index = activeIndex + 1;
            var pageEm = null;
            if(_index > index){//向下滑动
                pageEm = $("div[data-index='"+_index+"']").next('div');
            }else{//向上滑动
                pageEm = $("div[data-index='"+_index+"']").prev('div');
            }
            if(pageEm.length >0){
                var pageNo = $(pageEm).data('no');
                loadPageList(pageNo);
            }
            index = _index;
        });
    });

    //加载页面数据
    function loadPageList(_pageNo,callback){
        //加载试题列表
        loadQuestionList({
            'examRecordId':examRecordId,
            'status':status,
            'pageNo':_pageNo,
            'callback':callback
        });
    }

    //点击某答题卡序号，切换到对应序号的题目
    $('#AnswerCard-popup-num-list').on('click', '.exam-option-bx', function () {
        var _this = $(this),
            _name = (_this.data('name')) -1;
        $.closeModal('.AnswerCard-Popup');  //关闭答题卡 Popup
        var pageNo = _this.data('no');
        loadPageList(pageNo,function () {
            eSwiper.slideTo(_name, 200, true);  //切换到第 _name 个slide，速度为0.2秒; 并触发onSlideChange回调函数
        });
    });

    $("body").on('click','#finish-course-exercise',function () {//完成练习
        var saveId = getSaveId(0);
        _saveQuestionRecord(0,saveId,function () {
            var examRecordId = $("#examRecordId").val();
            var courseId = $("#courseId").val();
            var catalogId = $("#catalogId").val();
            $.ajax({
                url: baseloaction + '/mobile/uc/play/exercises/saveExercises',
                data: {
                    'courseId':courseId,
                    'catalogId':catalogId,
                    'examRecordId':examRecordId
                },
                dataType: 'json',
                type: 'post',
                success: function (response) {
                    if (response.success==true) {
                        var text = "";
                        var yes = '';
                        var cancel = '';
                        if(response.adopt==true){
                            text +="<p class='c-green'>你已通过随堂练习</p>";
                            yes = '返回继续学习';
                        }else{
                            text +="<p class='c-red'>你未通过随堂练习</p>";
                            yes = '继续答题';
                            cancel = '不想再学';
                        }
                        text +="<span class='fs6'>要求得分率："+response.scorePercent+"%，</span>";
                        text +="<span class='fs6'>你的得分率："+response.userPercent+"%</span>";
                        if(response.adopt==true){
                            Zepto.alert(text,function () {
                                window.location.href = baseloaction +'/mobile/course/info?onLimit=true&courseId='+courseId;
                            });
                        }
                        if (response.adopt==false){
                            Zepto.confirm(text, function () {
                                popupExerciseConfirm(courseId,catalogId,null,0,function (_id) {
                                    window.location.href = baseloaction + '/mobile/uc/play/exercises/loadExercises?examRecordId='+_id;
                                });
                            },function () {
                                window.location.href = baseloaction +'/mobile/course/info?onLimit=true&courseId='+courseId;
                            });
                        }
                        Zepto('.modal-in').find('.modal-button').text(cancel).css('color', '#bbb');
                        Zepto('.modal-in').find('.modal-button-bold').text(yes).attr('style', '');
                    } else {
                        $.toast(response.message);
                    }
                },
                error:function (error) {
                    $.toast('系统错误，提交随堂练习失败');
                }
            });
        });
    }).on('click','li>label.click-option-box',function () {//选择选项
        var _this = $(this);
        var typeValue = _this.data('type');
        if(typeValue == 2){//多选
            if(_this.hasClass('current')){
                _this.removeClass('current');
            }else{
                _this.addClass('current');
            }
        }else{//单选
            _this.addClass('current').siblings().removeClass("current");
        }
        saveQuestionRecord(this);
        return false;
    }).on('blur','.data-answers-2',function () {//填空、简答题鼠标离开的时候
        saveQuestionRecord(this);
        return false;
    });

    /**
     * 保存试题答案
     * @param em
     */
    function saveQuestionRecord(em) {
        var status = $("#status").val();
        if(status!=1){
            return;
        }
        $("#answerCard_"+ $("#exam-active-number").text()).addClass("exam-option-disabled");
        //试题根元素
        var _root = $($(em).parents('div.question-root-box')[0]);
        var currentId = _root.data('id');//考试试题记录ID
        var saveId = getSaveId(currentId);
        _saveQuestionRecord(currentId,saveId);
    }

    //执行保存试题作答记录
    function _saveQuestionRecord(currentId,saveId,callback) {
        if(saveId > 0){
            var root = $($("div.question-root-box[data-id='"+saveId+"']")[0]);
            var userAnswer = getAnswer(root);
            var examRecordId = $("#examRecordId").val();
            userAnswer = encodeURIComponent(JSON.stringify(userAnswer));
            saveQuestionData(examRecordId,saveId,userAnswer,function () {
                $("#card-"+saveId).addClass('exam-option-disabled');
                updateSaveData(saveId,currentId,callback);
            });
        }else{
            updateSaveData(saveId,currentId,callback);
        }
    }
});
